package com.example.kliven.because.fragment;

import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TabHost;
import android.widget.TextView;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.example.kliven.because.R;
import com.example.kliven.because.activity.BaiduMapActivity;
import com.example.kliven.because.adapter.HomeTabAdapter;

public class HomeFragment extends Fragment implements View.OnClickListener {

    private TabHost homeTabHost;
    private FragmentManager fm;
    private Toolbar toolbar;

    private ImageView mTabLine; // 指导线
    private int screenWidth; // 屏幕的宽度

    private ViewPager viewPager;
    private RadioGroup radioGroup;
    private RadioButton radioButton1, radioButton2;
    private HomeTabAdapter adapter;

    private DrawerLayout drawerLayout;

    public static final int TAB_SHARE = 0;
    public static final int TAB_COMMENT = 1;

    private TextView tv_showCity;
    private ProgressDialog dialog;

    public LocationClient mLocationClient;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.home_fragment, null);
//        showTabHost(view);
        view.findViewById(R.id.open_camera).setOnClickListener(this);
        initView(view);
        addListener();

        tv_showCity = (TextView) view.findViewById(R.id.show_city);
        view.findViewById(R.id.left_menu).setOnClickListener(this);

        mLocationClient = new LocationClient(getActivity());
        // 声明LocationClient类
        mLocationClient.registerLocationListener(new MyLocationListener()); // 注册监听函数
        LocationClientOption option = new LocationClientOption();
        option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);// 设置定位模式
        option.setCoorType("bd09ll");// 返回的定位结果是百度经纬度,默认值gcj02
        option.setScanSpan(5000);// 设置发起定位请求的间隔时间为5000ms
        option.setIsNeedAddress(true);// 返回的定位结果包含地址信息
        option.setNeedDeviceDirect(true);// 返回的定位结果包含手机机头的方向
        mLocationClient.setLocOption(option);
        mLocationClient.start();
        return view;

    }

    private void initView(View view) {
//        drawerLayout = (DrawerLayout) view.findViewById(R.id.left_drawer_lv);
        viewPager = (ViewPager) view.findViewById(R.id.viewpager);
        radioGroup = (RadioGroup) view.findViewById(R.id.radio_group);
        radioButton1 = (RadioButton) view.findViewById(R.id.radio_btn_share);
        radioButton2 = (RadioButton) view.findViewById(R.id.radio_btn_comment);
        view.findViewById(R.id.left_menu).setOnClickListener(this);
        mTabLine = (ImageView) view.findViewById(R.id.id_tab_line);

        // 获取屏幕长度
        DisplayMetrics outMetrics = new DisplayMetrics();
        getActivity().getWindow().getWindowManager().getDefaultDisplay()
                .getMetrics(outMetrics);
        screenWidth = outMetrics.widthPixels;
        //设置mTabLine 宽度 获取组件的
        //获取空间的布局参数对象
        LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) mTabLine.getLayoutParams();
        lp.width = screenWidth / 2;
        mTabLine.setLayoutParams(lp); // 设置该空间的layoutParams参数;

        radioGroup.check(R.id.radio_btn_share);
        radioButton1.setOnClickListener(this);
        radioButton2.setOnClickListener(this);

        adapter = new HomeTabAdapter(getActivity().getSupportFragmentManager());
        viewPager.setAdapter(adapter);
    }

    private void addListener() {
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int id) {
                switch (id) {
                    case TAB_SHARE:
                        radioButton1.setChecked(true);
                        break;
                    case TAB_COMMENT:
                        radioButton2.setChecked(true);
                        break;
                }
            }

            @Override
            public void onPageScrolled(int position, float arg1, int arg2) {

                LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) mTabLine.getLayoutParams();
                // 获取组件距离左侧组件的距离
                lp.leftMargin = ((arg2 + position)*screenWidth / 2);
                mTabLine.setLayoutParams(lp);
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {

            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.radio_btn_share:
                viewPager.setCurrentItem(TAB_SHARE);
                break;
            case R.id.radio_btn_comment:
                viewPager.setCurrentItem(TAB_COMMENT);
                break;
            case R.id.open_camera:
                openSysCamera();
                break;
            case R.id.left_menu:
                Intent intent = new Intent(getActivity(), BaiduMapActivity.class);
                startActivity(intent);
                break;
        }
    }
    private void openSysCamera() {
        Intent openCameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivity(openCameraIntent);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        toolbar = (Toolbar) view.findViewById(R.id.toolbar_home_page);
        toolbar.setTitle("");
        toolbar.setBackgroundColor(Color.parseColor("#1C2133"));
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        ((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
        setHasOptionsMenu(true);
    }


    class MyLocationListener implements BDLocationListener {
        @Override
        public void onReceiveLocation(BDLocation location) {
            StringBuffer sb = new StringBuffer(256);
            sb.append("time : ");
            sb.append(location.getTime());
            sb.append("\nerror code : ");
            sb.append(location.getLocType());
            sb.append("\nlatitude : ");
            sb.append(location.getLatitude());
            sb.append("\nlontitude : ");
            sb.append(location.getLongitude());
            sb.append("\nradius : ");
            sb.append(location.getRadius());
            sb.append("\ncity : ");
            sb.append(location.getCity());
            sb.append("\nprovince : ");
            sb.append(location.getProvince());
            if (location.getLocType() == BDLocation.TypeGpsLocation) {
                sb.append("\nspeed : ");
                sb.append(location.getSpeed());
                sb.append("\nsatellite : ");
                sb.append(location.getSatelliteNumber());
            } else if (location.getLocType() == BDLocation.TypeNetWorkLocation) {
                sb.append("\naddr : ");
                sb.append(location.getAddrStr());
            }
            System.out.println(sb.toString());
            Log.e("MyLocationListener", sb.toString());
            tv_showCity.setText(location.getCity());
        }
    }
}